package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiantu.phone.R;
import com.qiantu.phone.other.LimitCharLengthTextWatcher;
import com.qiantu.phone.widget.EditTextLayout;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.KeyboardUtils;

public class BottomEditDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private EditTextLayout mEditText;
    private TextView tvLabel;
    private TextView btnSave;
    private String text = "";
    private String hint;
    private String label;
    private String btnText;
    private String unitText;
    private int maxLength;
    private int inputType = -1;
    private boolean onConfirmDismiss = true;
    private boolean checkEnable = true;
    private TextView tvUnit;
    public BottomEditDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_edit;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        tvLabel = findViewById(R.id.tv_label);
        mEditText = findViewById(R.id.edit_name);
        btnSave = findViewById(R.id.btn_save);
        tvUnit = findViewById(R.id.unit);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardUtils.hideSoftInput(mEditText.getEditText());
                if(onConfirmDismiss){
                    dismiss();
                }
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick();
                }
            }
        });
        tvLabel.setText(label);
        tvUnit.setText(unitText);
        if(!TextUtils.isEmpty(btnText)){
            btnSave.setText(btnText);
        }
        if(!TextUtils.isEmpty(hint)){
            mEditText.setHint(hint);
        }
        if(inputType != - 1){
            mEditText.getEditText().setInputType(inputType);
        }
        if(maxLength != 0){
//            InputFilter[] filters = { new InputFilter.LengthFilter(maxLength) };
//            mEditText.getEditText().setFilters(filters);
            mEditText.getEditText().addTextChangedListener(new LimitCharLengthTextWatcher(mEditText.getEditText(), maxLength));
        }
        mEditText.setText(text);
        if(!TextUtils.isEmpty(text)){
            mEditText.getEditText().setSelection(mEditText.getText().length());
        }
        btnSave.setEnabled(!checkEnable);
        mEditText.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String textStr = s.toString();
                if (textStr.isEmpty() || (text.equals(textStr) && checkEnable)){
                    btnSave.setEnabled(false);
                }else {
                    btnSave.setEnabled(true);
                }
            }
        });
    }

    public void setOnConfirmDismiss(boolean onConfirmDismiss) {
        this.onConfirmDismiss = onConfirmDismiss;
    }

    public void setInputType(int inputType){
        this.inputType = inputType;
        if(mEditText != null){
            mEditText.getEditText().setInputType(inputType);
        }
    }

    public void setLabel(String label){
        this.label = label;
        if(tvLabel != null){
            tvLabel.setText(label);
        }
    }

    public void setUnit(String unit){
        unitText = unit;
        if (tvUnit != null){
            tvUnit.setText(unitText);
        }
    }

    public void setBtnText(String btnText){
        this.btnText = btnText;
        if(btnSave != null){
            btnSave.setText(btnText);
        }
    }

    public void setCheckEnable(Boolean checkEnable){
        this.checkEnable = checkEnable;
        if(!checkEnable && btnSave != null){
            btnSave.setEnabled(true);
        }
    }

    public void setMaxLength(int maxLength){
        this.maxLength = maxLength;
        if(mEditText != null){
//            InputFilter[] filters = { new InputFilter.LengthFilter(maxLength) };
//            mEditText.getEditText().setFilters(filters);
            mEditText.getEditText().addTextChangedListener(new LimitCharLengthTextWatcher(mEditText.getEditText(), maxLength));
        }
    }

    public void setText(String text){
        if(text == null){
            text = "";
        }
        this.text = text;
        if(mEditText != null){
            mEditText.setText(text);
            mEditText.getEditText().setSelection(mEditText.getText().length());
        }
    }

    public void setHint(String hint){
        this.hint = hint;
        if(mEditText != null){
            mEditText.setHint(hint);
        }
    }

    public String getText() {
        if(mEditText != null){
            text = mEditText.getText().toString();
        }
        return text;
    }
    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void beforeShow() {
        super.beforeShow();
        showSoftInput(mEditText.getEditText());
    }

    public interface DialogClickListener {
        void onConfirmClick();
    }

    @Override
    protected void beforeDismiss() {
        super.beforeDismiss();
        KeyboardUtils.hideSoftInput(mEditText.getEditText());
    }
}
